/*
The MIT License
Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE

This file is part of embeddedRTPS.

Author: i11 - Embedded Software, RWTH Aachen University
*/

#ifndef RTPS_LOCATOR_T_H
#define RTPS_LOCATOR_T_H

#include "rtps/communication/UdpDriver.h"
#include "rtps/utils/udpUtils.h"
#include "ucdr/microcdr.h"

typedef enum LocatorKind_t {
  LOCATOR_KIND_INVALID = -1,
  LOCATOR_KIND_RESERVED = 0,
  LOCATOR_KIND_UDPv4 = 1,
  LOCATOR_KIND_UDPv6 = 2
}LocatorKind_t;

//const uint32_t LOCATOR_PORT_INVALID = 0;
//const uint8_t LOCATOR_ADDRESS_INVALID[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

typedef struct Locator {
  enum LocatorKind_t kind; //default LOCATOR_KIND_INVALID;
  uint32_t port;//default LOCATOR_PORT_INVALID;
  uint8_t address[16] ; // default LOCATOR_ADDRESS_INVALID TODO make private such that kind and address
                               // always match?
}Locator;

inline void rtps_Locator_createUDPv4Locator(struct Locator *locator, uint8_t a, uint8_t b, uint8_t c, 
                                      uint8_t d, uint32_t port) {
  locator->kind = LOCATOR_KIND_UDPv4;
  locator->address[12] = a;
  locator->address[13] = b;
  locator->address[14] = c;
  locator->address[15] = d;
  locator->port = port;
}

inline void rtps_Locator_setInvalid(struct Locator *pLocator){
  pLocator->kind = LOCATOR_KIND_INVALID; 
}

inline bool rtps_Locator_isValid(struct Locator *pLocator){
  return pLocator->kind != LOCATOR_KIND_INVALID; 
}

inline bool rtps_Locator_readFromUcdrBuffer(struct Locator *pLocator, ucdrBuffer *buffer) {
  if (ucdr_buffer_remaining(buffer) < sizeof(struct Locator)) {
    return false;
  } else {
    ucdr_deserialize_array_uint8_t(buffer, (uint8_t *)pLocator,
                                    sizeof(struct Locator));
    return true;
  }
}

inline bool rtps_Locator_serializeIntoUdcrBuffer(struct Locator *pLocator, ucdrBuffer *buffer) {
  if (ucdr_buffer_remaining(buffer) < sizeof(struct Locator)) {
    return false;
  } else {
    return ucdr_serialize_array_uint8_t(buffer, (uint8_t *)pLocator, sizeof(struct Locator));
  }
}

inline ip4_addr_t rtps_Locator_getIp4Address(struct Locator *pLocator){
  return transformIP4ToU32(pLocator->address[12], pLocator->address[13], 
                          pLocator->address[14], pLocator->address[15]);
}

inline bool rtps_Locator_isSameAddress(struct Locator *pLocator, ip4_addr_t *address) {
  ip4_addr_t ownaddress = rtps_Locator_getIp4Address(pLocator);
  return ip4_addr_cmp(&ownaddress, address);
}

inline uint32_t rtps_Locator_getLocatorPort(struct Locator *pLocator){ 
  return pLocator->port;
}

inline bool rtps_Locator_isSameSubnet(struct Locator *pLocator){
  return rtps_UdpDriver_isSameSubnet(rtps_Locator_getIp4Address(pLocator));
}

inline void rtps_Locator_getBuiltInUnicastLocator(struct Locator *pLocator, ParticipantId_t participantId) {
  rtps_Locator_createUDPv4Locator(pLocator,
      IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2],
      IP_ADDRESS[3], getBuiltInUnicastPort(participantId));
}

inline void rtps_Locator_getBuiltInMulticastLocator(struct Locator *pLocator) {
  rtps_Locator_createUDPv4Locator(pLocator, 239, 255, 0, 1, getBuiltInMulticastPort());
}

inline void rtps_Locator_getUserUnicastLocator(struct Locator *pLocator, ParticipantId_t participantId) {
  rtps_Locator_createUDPv4Locator(pLocator, 
      IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2],
      IP_ADDRESS[3], getUserUnicastPort(participantId));
}

inline void rtps_Locator_getUserMulticastLocator(struct Locator *pLocator) { // this would be a unicastaddress, as
                                           // defined in config
  rtps_Locator_createUDPv4Locator(pLocator,
      IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2],
      IP_ADDRESS[3], getUserMulticastPort());
}

inline void rtps_Locator_getDefaultSendMulticastLocator(struct Locator *pLocator) {
  rtps_Locator_createUDPv4Locator(pLocator, 239, 255, 0, 1, getBuiltInMulticastPort());
}

#endif // RTPS_LOCATOR_T_H
